/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.terminalrodoviario;
import br.*;
import java.util.Collection;
/**
 *
 * @author marcelo
 */
public class BaseEntity<t> implements IBaseEntity {

    // <editor-fold defaultstate="collapsed" desc="Properties">
    private int id;
    private boolean removed;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Gets and Sets">
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public boolean isRemoved() {
        return removed;
    }

    public void setRemoved(boolean removed) {
        this.removed = removed;
    }
   
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="public methods">
    public void insert(t obj){
        Db db = new Db();
        insert(obj,db);
        db.commit();
        db.disconnect();        
    }
     
    public void insert (t obj,Db db){
         db.insert(obj);
     }

    public void insert() {
       insert((t)this);
    }
    
    public void delete(t obj,Db db){
        db.delete(obj);
    }

    public void delete(t obj){
        Db db = new Db();
        delete(obj,db);
        db.commit();       
    }

    public void delete() {
        delete((t)this);
    }

    public void update(t obj){
        Db db = new Db();
        update(obj,db);
        db.commit();
        db.disconnect();
    }

    public void update(t obj,Db db){
        db.update(obj);
    }
  
    public void update() {
       update((t)this);
    }

    public Collection<t> select(String query,EQueryType type){
        Db db = new Db();
        db.connect();
        Collection<t> lst = db.select(query,type);
        db.disconnect();
        return lst;
    }

    public Collection<t> select(String queryName,String[] paramNames, Object... paramValues){
        Db db = new Db();
        db.connect();
        Collection<t> lst = db.selectNamedQuery(queryName,paramNames,paramValues);
        db.disconnect();
        return lst;
    }
    
    public Collection<t> select(String query){
        return select(query,EQueryType.Query);
    }

    public t selectById(String query){
        Db db = new Db();
        db.connect();
        Collection<t> lst = db.select(query);
        db.disconnect();
        return (t)lst.toArray()[0];
    }
    // </editor-fold>
}
